/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmCrearHotel.java
 *
 * Created on 19-feb-2011, 20:02:06
 */

package Presentacion;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author edu
 */
public class frmEditarHotel extends javax.swing.JFrame {

    /** Creates new form frmCrearHotel */
    int CodigoCiudad;
    int CodigoHotel;
    public frmEditarHotel(int Codigo, int codHotel) {
        initComponents();
        CodigoCiudad=Codigo;
        lblerror.setVisible(false);
        lblTelError.setVisible(false);
        setTitle("ManchaViajes: Nuevo Hotel");
        CodigoHotel=codHotel;
        CargarDatos();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        txtNumEstrellas = new javax.swing.JTextField();
        txtDireccion = new javax.swing.JTextField();
        txtTelefono = new javax.swing.JTextField();
        btnCrear = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lblerror = new javax.swing.JLabel();
        lblTelError = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Nombre: ");

        jLabel2.setText("Número de Estrellas:");

        jLabel3.setText("Dirección:");

        jLabel4.setText("Teléfono:");

        btnCrear.setText("Aceptar");
        btnCrear.setToolTipText("Aceptar");
        btnCrear.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnCrearMouseClicked(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.setToolTipText("Cancelar");
        btnCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnCancelarMouseClicked(evt);
            }
        });

        lblerror.setBackground(new java.awt.Color(255, 0, 20));
        lblerror.setFont(new java.awt.Font("Comic Sans MS", 0, 10));
        lblerror.setForeground(new java.awt.Color(255, 0, 0));
        lblerror.setText("Numero de estrellas incorrecto");
        lblerror.setFocusable(false);

        lblTelError.setBackground(new java.awt.Color(255, 0, 20));
        lblTelError.setFont(new java.awt.Font("Comic Sans MS", 0, 10));
        lblTelError.setForeground(new java.awt.Color(255, 0, 0));
        lblTelError.setText("Teléfono erróneo");
        lblTelError.setAlignmentY(0.0F);
        lblTelError.setFocusable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(18, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(lblerror))
                    .addComponent(txtNumEstrellas, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(btnCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnCancelar))
                            .addComponent(lblTelError))))
                .addGap(34, 34, 34))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNumEstrellas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblerror)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(lblTelError))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnCancelar)
                            .addComponent(btnCrear))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCancelarMouseClicked
        setVisible(false);
    }//GEN-LAST:event_btnCancelarMouseClicked

    private void btnCrearMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCrearMouseClicked
        
        lblTelError.setVisible(false);
        lblerror.setVisible(false);

        //Oculta el label de error por si ya se ha introducido el correcto
        String vacio=new JTextField().getText();
        if(txtNombre.getText().equals(vacio) || txtDireccion.getText().equals(vacio) || txtNumEstrellas.getText().equals(vacio) || txtTelefono.getText().equals(vacio))
        {
            JOptionPane.showMessageDialog(null,"Todos los datos son necesarios", "Campos vacios",JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            int Estrellas=0;
            //Se comprueba que el numero de estrellas es un digito
            boolean correcto=true;
            try{
                Estrellas=Integer.parseInt(txtNumEstrellas.getText());
            }catch(Exception e){
                correcto=false;
                lblerror.setVisible(true);
            }

            if(correcto)
            {
                try{
                    Integer.parseInt(txtTelefono.getText());
                }catch(Exception e){
                    lblTelError.setVisible(true);
                    correcto=false;
                }

                if(correcto)
                {
                   frmPrincipal.agencia.Ciudades.get(CodigoCiudad).Hoteles.get(CodigoHotel).setNombre(txtNombre.getText());
                   frmPrincipal.agencia.Ciudades.get(CodigoCiudad).Hoteles.get(CodigoHotel).setDireccion(txtDireccion.getText());
                   frmPrincipal.agencia.Ciudades.get(CodigoCiudad).Hoteles.get(CodigoHotel).setEstrellas(Estrellas);
                   frmPrincipal.agencia.Ciudades.get(CodigoCiudad).Hoteles.get(CodigoHotel).setTelefono(txtTelefono.getText());

                   frmTransyHotel.listHoteles.setListData(frmPrincipal.agencia.Ciudades.get(CodigoCiudad).Hoteles.toArray());
                   dispose();
                }
            }
        }
    }//GEN-LAST:event_btnCrearMouseClicked

    public final void CargarDatos()
    {
        Hotel h=frmPrincipal.agencia.Ciudades.get(CodigoCiudad).Hoteles.get(CodigoHotel);
        txtNombre.setText(h.Nombre);
        txtDireccion.setText(h.Direccion);
        txtNumEstrellas.setText(String.valueOf(h.Estrellas));
        txtTelefono.setText(String.valueOf(h.Telefono));
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnCrear;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel lblTelError;
    private javax.swing.JLabel lblerror;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtNumEstrellas;
    private javax.swing.JTextField txtTelefono;
    // End of variables declaration//GEN-END:variables

}
